package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type R struct {
	Success bool                   `json:"success"`
	Code    int                    `json:"code"`
	Message string                 `json:"message"`
	Data    map[string]interface{} `json:"data"`
}

const (
	SUCCESS int = 20000 //操作成功
	ERROR   int = 20001 //操作失败
)

func Response(context *gin.Context, data interface{}) {
	context.JSON(http.StatusOK, data)
}
func Success(context *gin.Context, v interface{}) {
	Response(context, v)
}
func Failed(context *gin.Context, v interface{}) {
	Response(context, v)
}
func Ok() R {
	var r R = R{}
	r.Success = true
	r.Code = SUCCESS
	r.Message = "成功"
	r.Data = make(map[string]interface{})
	return r
}
func Error() R {
	var r R = R{}
	r.Success = false
	r.Code = ERROR
	r.Message = "失败"
	r.Data = make(map[string]interface{})
	return r
}
func (r R) AddData(key string, obj interface{}) R {
	r.Data[key] = obj
	return r
}
func (r R) SetData(data map[string]interface{}) R {
	r.Data = data
	return r
}
